package com.qiy.springcloud.lb;

import org.springframework.cloud.client.ServiceInstance;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created on 2020/4/6.
 *
 * @author sehuaqi.
 * 描述：Ribbon 手写轮询算法
 */
@Component
public class MyLB implements LoadBalancer {

    //原子类
    private AtomicInteger atomicInteger = new AtomicInteger(0);

    @Override
    public ServiceInstance instances(List<ServiceInstance> serviceInstanceList) {
        int index = getAndIncrement() % serviceInstanceList.size();
        return serviceInstanceList.get(index);
    }

    private final int getAndIncrement() {

        int current;
        int next;
        do {
            current = this.atomicInteger.get();
            //超过最大值，为0，重新计数 2147483647 Integer.MAX_VALUE，不直接写方法是因为方法运行时每次都会创建一个栈区，而写死就不会，涉及Jvm调优
            next = current >= 2147483647 ? 0 : current +1;
            // 自旋锁
        } while (!this.atomicInteger.compareAndSet(current, next));

        System.out.println(">>>>>> 第" + next + "次访问");

        return next;
    }
}
